﻿#region MIT Licence
/*
 * Copyright (C) 2014 PdfCreative
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
 * and associated documentation files (the "Software"), to deal in the Software without restriction, 
 * including without limitation the rights to use, copy, modify, merge, publish, distribute, 
 * sublicense, and/or sell copies of the Software, and to permit persons to whom 
 * the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included 
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE 
 * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
#endregion

using System;
using System.Collections.Generic;
using System.Text;

namespace PdfCreative.Pdf.Graphics
{
    /// <summary>
    /// The Vector class represents a list of numbers. It is used to transform 2D coordinates
    /// using a coordinate transformation matrix.
    /// </summary>
    public class Vector
    {
        // Private fields
        // ==============
        #region Private fields
        private float[] _vector;            // The values of this vector instance.
        #endregion



        // Public properties
        // =================
        #region Public properties
        /// <summary>
        /// Sets or retrieves the value at the specified index.
        /// </summary>
        /// <param name="index">The index.</param>
        /// <returns>The value at that point inside the vector.</returns>
        /// <exception cref="ArgumentOutOfRangeException">The index is out of range.</exception>
        public float this[int index]
        {
            get { return _vector[index]; }
            set { _vector[index] = value; }
        }

        /// <summary>
        /// Retrieves the length of the vector.
        /// </summary>
        public int Length
        {
            get { return _vector.Length; }
        }
        #endregion



        // Public methods
        // ==============
        #region Public methods
        /// <summary>
        /// Copies the specified values into the vector. If there are more values than the vector
        /// can hold, the list is truncated. No exception will be thrown.
        /// </summary>
        /// <param name="values">The values.</param>
        /// <exception cref="ArgumentNullException">The values array can not be a Null reference.</exception>
        public void SetValues(params float[] values)
        {
            if (null == values) throw new ArgumentNullException("values");

            int index = 0;
 
            // Go through all the values in the list, and insert them into the vector.
            foreach (float value in values)
            {
                // Insert into the matrix
                _vector[index] = value;

                // If this was the last value to fit, we cancel the operation.
                index++;
                if (index >= this.Length) break;
            }
        } // SetValues()
        #endregion



        // Public constructor
        // ==================
        #region Public constructor
        /// <summary>
        /// Constructs a new Vector instance with the specified length.
        /// </summary>
        /// <param name="length">The length of the vector.</param>
        public Vector(int length)
        {
            _vector = new float[length];
        }

        /// <summary>
        /// Constructs a new Vector instance with the specified values.
        /// If there are more values than the vector can hold, the list is truncated.
        /// No exception will be thrown.
        /// </summary>
        /// <param name="values">The values for the vector.</param>
        public Vector(params float[] values)
        {
            _vector = new float[values.Length];
            SetValues(values);
        }

        #endregion

    } // Vector class
}
